package com.codemonkey.lq.common.util;

import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.ListIterator;
import java.util.Map;

import com.google.common.base.Predicate;

public final class CollectionHelper {

	/**
	 * @param <E>
	 * @param args
	 *            可变参数
	 * @return
	 */
	public static <E> List<E> gather(E... args) {
		return Arrays.asList(args);
	}

	/**
	 * 移除满足条件的Element
	 * @param <E>
	 * @param list
	 * @param predicate
	 */
	public static <E> void remove(List<E> list, Predicate<E> predicate) {
		// http://www.hankcs.com/program/java/java-collection-traversing-delete-merge-elements.html
		// https://stackoverflow.com/questions/10431981/remove-elements-from-collection-while-iterating
		ListIterator<E> listIterator = list.listIterator();
		while (listIterator.hasNext()) {
			final E next = listIterator.next();
			if (predicate.apply(next)) {
				listIterator.remove();
			}
		}
	}
	
	private boolean isUnmodifiableMap(Map<String, Object> map) {
		boolean isTrue = false;
		try {
			map.put("______", "dsad");
		} catch (UnsupportedOperationException e) {
			isTrue = true;
		}

		if (isTrue) {
			return true;
		} else {
			map.remove("______");
			return false;
		}
	}	
	
	private boolean isUnmodifiableCollection(Collection<Object> collection) {
		boolean isTrue = false;
		try {
			collection.add("_______");
		} catch (UnsupportedOperationException e) {
			isTrue = true;
		}

		if (isTrue) {
			return true;
		} else {
			collection.remove("_______");
			return false;
		}

	}	
}
